/*
 *	Copyright 2011 ja4cli
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 */
package org.skarby.ja4cli.internal;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

/**
 * Identifies applicaple types of segments in an arguments array.<p/>
 * 
 * The identification process is very rough and imprecise, such as:
 * <ul>
 * <li>Context (position) of an argument is not always taken into consideration</li>
 * <li></li>
 * </ul>
 * Basicly, without further inspection of arguments in relation to a configuration of some sorts,
 * the context of arguments cannot be desided.
 * 
 * @author askarby
 */
public class ArgumentSegmentIdentification {

	/**
	 * Identifies the types of segments that are applicable to a given argument array.
	 * @param arguments the arguments to identify segment types for, may <b>not</b> be <code>null</code>.
	 * @return
	 */
	public List<Set<ArgumentSegmentType>> identify(String[] arguments) {
		Guard.isNull(arguments);

		List<Set<ArgumentSegmentType>> identifications = new ArrayList<Set<ArgumentSegmentType>>(arguments.length);
		for (int i=0; i<arguments.length; i++) {
			String current = arguments[i];
			String next = i == arguments.length - 1 ? null : arguments[i+1];
			
			EnumSet<ArgumentSegmentType> matches = ArgumentSegmentType.possibleMatches(current);
			if (i == 0) {
				// The first element
				matches.remove(ArgumentSegmentType.PARAMETER_VALUE);
				if (next == null) {
					matches.remove(ArgumentSegmentType.PARAMETER_DEFINITION);
				}
			} else if (i == arguments.length - 1) {
				// The last element
				matches.remove(ArgumentSegmentType.PARAMETER_DEFINITION);
			}
			identifications.add(matches);
		}
		return identifications;
	}
}
